package com.demo.java.OD251_300.OD277;

import java.util.*;
import java.io.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【快递投放问题】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146075833
 */
public class OdMain {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line1 = br.readLine().split(" ");
        int m = Integer.parseInt(line1[0]);
        int n = Integer.parseInt(line1[1]);

        List<String[]> want = new ArrayList<>();
        List<String[]> cant = new ArrayList<>();

        // 读取想要传输的路径
        for (int i = 0; i < m; i++) {
            String[] arr = br.readLine().split(" ");
            want.add(arr);
        }

        // 读取限制的路径
        for (int i = 0; i < n; i++) {
            String[] arr = br.readLine().split(" ");
            cant.add(arr);
        }

        // 调用方法处理并输出结果
        System.out.println(solve_method(want, cant));
    }

    public static String solve_method(List<String[]> want, List<String[]> cant) {
        // 用于存储每个路径和它对应的包裹
        Map<String, Set<String>> want_map = new HashMap<>();
        Map<String, Set<String>> cant_map = new HashMap<>();

        // 处理想要传输的包裹信息
        for (String[] arr : want) {
            String pkg = arr[0];
            String path = arr[1] + "-" + arr[2];
            if (!want_map.containsKey(path)) {
                want_map.put(path, new HashSet<>());
            }
            want_map.get(path).add(pkg);
        }

        // 处理限制的包裹信息
        for (String[] arr : cant) {
            String path = arr[0] + "-" + arr[1];
            if (!cant_map.containsKey(path)) {
                cant_map.put(path, new HashSet<>());
            }
            for (int i = 2; i < arr.length; i++) {
                cant_map.get(path).add(arr[i]);
            }
        }

        // 存储结果
        List<String> res = new ArrayList<>();

        // 检查每个想要传输的包裹是否被限制
        for (Map.Entry<String, Set<String>> entry : want_map.entrySet()) {
            String path = entry.getKey();
            Set<String> want_pkg = entry.getValue();
            Set<String> cant_pkg = cant_map.get(path);
            if (cant_pkg == null) {
                continue;
            }
            // 如果包裹在限制中，加入结果列表
            for (String pkg : want_pkg) {
                if (cant_pkg.contains(pkg)) {
                    res.add(pkg);
                }
            }
        }

        // 如果没有受到限制的包裹，返回none
        if (res.isEmpty()) {
            return "none";
        }

        // 对结果按照包裹编号排序
        Collections.sort(res, (a, b) -> Integer.parseInt(a.substring(7)) - Integer.parseInt(b.substring(7)));

        // 返回结果
        return String.join(" ", res);
    }
}